package com.randi.file.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;

import java.util.Arrays;
import java.util.stream.Stream;

/**
 * @author ziye
 */

public enum MineEnum {

    /**
     * 图片
     */
    $JPEG("image/jpeg", "jpeg", FileTypeEnum.IMAGE.name()),
    $JPG("image/jpeg", "jpg", FileTypeEnum.IMAGE),
    $PNG("image/png", "png", FileTypeEnum.IMAGE),
    $GIF("image/gif", "gif", FileTypeEnum.IMAGE),
    $SVG("image/svg+xml", "svg", FileTypeEnum.IMAGE),
    $SVGZ("image/svg+xml", "svgz", FileTypeEnum.IMAGE),
    $TIFF("image/tiff", "tiff", FileTypeEnum.IMAGE),
    $BMP("image/bmp", "bmp", FileTypeEnum.IMAGE),
    $ICO("image/x-icon", "ico", FileTypeEnum.IMAGE),
    $WEBP("image/webp", "webp", FileTypeEnum.IMAGE),

    /**
     * 音频
     */
    $MP3("audio/mpeg", "mp3", FileTypeEnum.AUDIO),
    $WAV("audio/wav", "wav", FileTypeEnum.AUDIO),
    $AAC("audio/aac", "aac", FileTypeEnum.AUDIO),
    $OGG("audio/ogg", "ogg", FileTypeEnum.AUDIO),
    $M4A("audio/x-m4a", "m4a", FileTypeEnum.AUDIO),
    $FLAC("audio/flac", "flac", FileTypeEnum.AUDIO),

    /**
     * 视频
     */
    $MP4("video/mp4", "mp4", FileTypeEnum.VIDEO),
    $AVI("video/x-msvideo", "avi", FileTypeEnum.VIDEO),
    $WMV("video/x-ms-wmv", "wmv", FileTypeEnum.VIDEO),
    $MOV("video/quicktime", "mov", FileTypeEnum.VIDEO),
    $WEBM("video/webm", "webm", FileTypeEnum.VIDEO),
    $FLV("video/x-flv", "flv", FileTypeEnum.VIDEO),
    $RM("video/x-pn-realvideo", "rm", FileTypeEnum.VIDEO),
    $RMVB("video/x-pn-realvideo", "rmvb", FileTypeEnum.VIDEO),
    $ASF("video/x-ms-asf", "asf", FileTypeEnum.VIDEO),

    /**
     * 文档
     */
    $PDF("application/pdf", "pdf", FileTypeEnum.DOCUMENT),
    $PDFX("application/pdf", "pdfx", FileTypeEnum.DOCUMENT),
    $DOC("application/msword", "doc", FileTypeEnum.DOCUMENT),
    $DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", FileTypeEnum.DOCUMENT),
    $PPT("application/vnd.ms-powerpoint", "ppt", FileTypeEnum.DOCUMENT),
    $PPTX("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx", FileTypeEnum.DOCUMENT),
    $XLS("application/vnd.ms-excel", "xls", FileTypeEnum.DOCUMENT),
    $XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", FileTypeEnum.DOCUMENT),
    $ODT("text/x-openoffice", "odt", FileTypeEnum.DOCUMENT),
    $ODS("application/vnd.oasis.opendocument.spreadsheet", "ods", FileTypeEnum.DOCUMENT),
    $ODP("application/vnd.oasis.opendocument.presentation", "odp", FileTypeEnum.DOCUMENT),
    $ODG("application/vnd.oasis.opendocument.graphics", "odg", FileTypeEnum.DOCUMENT),
    $ODC("application/vnd.oasis.opendocument.chart", "odc", FileTypeEnum.DOCUMENT),

    /**
     * 压缩
     */
    $ZIP("application/zip", "zip", FileTypeEnum.ARCHIVE),
    $RAR("application/x-rar-compressed", "rar", FileTypeEnum.ARCHIVE),
    $TAR("application/x-tar", "tar", FileTypeEnum.ARCHIVE),
    $GZ("application/gzip", "gz", FileTypeEnum.ARCHIVE),
    $7Z("application/x-7z-compressed", "7z", FileTypeEnum.ARCHIVE),
    $ISO("application/x-iso9660-image", "iso", FileTypeEnum.ARCHIVE),
    $JAR("application/java-archive", "jar", FileTypeEnum.ARCHIVE),
    $WAR("application/java-archive", "war", FileTypeEnum.ARCHIVE),
    $EAR("application/java-archive", "ear", FileTypeEnum.ARCHIVE),
    $EBOOK("application/epub+zip", "epub", FileTypeEnum.ARCHIVE),

    $BIN("application/octet-stream", "bin", FileTypeEnum.ARCHIVE),
    $EXE("application/x-msdownload", "exe", FileTypeEnum.APP),
    $MSI("application/x-msdownload", "msi", FileTypeEnum.APP),
    $APK("application/vnd.android.package-archive", "apk", FileTypeEnum.APP),
    $RPM("application/x-rpm", "rpm", FileTypeEnum.APP),
    $DEB("application/x-debian-package", "deb", FileTypeEnum.APP),
    $DMG("application/x-apple-diskimage", "dmg", FileTypeEnum.APP),


    /**
     * 编码文件
     */
    $JSON("application/json", "json", FileTypeEnum.DOCUMENT),
    $XHTML("application/xhtml+xml", "xhtml", FileTypeEnum.DOCUMENT),
    $XML("application/xml", "xml", FileTypeEnum.DOCUMENT),
    $TXT("text/plain", "txt", FileTypeEnum.DOCUMENT),
    $CSV("text/csv", "csv", FileTypeEnum.DOCUMENT),
    $HTM("text/html", "htm", FileTypeEnum.DOCUMENT),
    $HTML("text/html", "html", FileTypeEnum.DOCUMENT),
    $HTML5("text/html", "html5", FileTypeEnum.DOCUMENT),
    $CSS("text/css", "css", FileTypeEnum.DOCUMENT),
    $SCSS("text/x-scss", "scss", FileTypeEnum.DOCUMENT),
    $LESS("text/x-less", "less", FileTypeEnum.DOCUMENT),
    $JS("text/javascript", "js", FileTypeEnum.DOCUMENT),
    $JSX("text/x-jsx", "jsx", FileTypeEnum.DOCUMENT),
    $TS("text/x-typescript", "ts", FileTypeEnum.DOCUMENT),
    $TSX("text/x-typescript", "tsx", FileTypeEnum.DOCUMENT),
    $PY("text/x-python", "py", FileTypeEnum.DOCUMENT),
    $JAVA("text/x-java", "java", FileTypeEnum.DOCUMENT),
    $PHP("text/x-php", "php", FileTypeEnum.DOCUMENT),
    $SQL("text/x-sql", "sql", FileTypeEnum.DOCUMENT),
    $VUE("text/x-vue", "vue", FileTypeEnum.DOCUMENT),
    $SH("text/x-shellscript", "sh", FileTypeEnum.DOCUMENT),
    $MD("text/x-markdown", "md", FileTypeEnum.DOCUMENT),
    $MDX("text/x-markdown", "mdx", FileTypeEnum.DOCUMENT),
    $BAT("application/bat", "bat", FileTypeEnum.DOCUMENT),
    $YML("text/x-yaml", "yml", FileTypeEnum.DOCUMENT),
    $YAML("text/x-yaml", "yaml", FileTypeEnum.DOCUMENT),
    $CLASS("application/java", "class", FileTypeEnum.DOCUMENT),
    $RB("text/x-ruby", "rb", FileTypeEnum.DOCUMENT),
    $PL("text/x-perl", "pl", FileTypeEnum.DOCUMENT),
    $TSV("text/tab-separated-values", "tsv", FileTypeEnum.DOCUMENT),

    /**
     * 字体
     */
    $WOFF("application/font-woff", "woff", FileTypeEnum.OTHER),
    $OTF("application/font-sfnt", "otf", FileTypeEnum.OTHER),
    $TTF("application/font-sfnt", "ttf", FileTypeEnum.OTHER),
    $EOT("application/vnd.ms-fontobject", "eot", FileTypeEnum.OTHER),
    $WOFF2("font/woff2", "woff2", FileTypeEnum.OTHER),
    $SFNT("application/font-sfnt", "sfnt", FileTypeEnum.OTHER),
    /**
     * 其他
     */
    $PSD("image/vnd.adobe.photoshop", "psd", FileTypeEnum.OTHER),
    $AI("application/postscript", "ai", FileTypeEnum.OTHER),
    $EPS("application/postscript", "eps", FileTypeEnum.OTHER),
    $PS("application/postscript", "ps", FileTypeEnum.OTHER),
    $OTHER("application/octet-stream", "", FileTypeEnum.OTHER);

    /**
     * 类型
     */
    @EnumValue
    private final String value;

    /**
     * 文件后缀
     */
    private final String fileSuffix;

    /**
     * 文件类型
     */
    private final FileTypeEnum fileType;

    private MineEnum(String value, String fileSuffix, FileTypeEnum fileType) {
        this.value = value;
        this.fileSuffix = fileSuffix;
        this.fileType = fileType;
    }

    private MineEnum(String value, String fileSuffix, String fileType) {
        this.value = value;
        this.fileSuffix = fileSuffix;
        this.fileType = FileTypeEnum.valueOfAuto(fileType);
    }

    public String getValue() {
        return value;
    }

    public String getFileSuffix() {
        return fileSuffix;
    }

    public FileTypeEnum getFileType() {
        return fileType;
    }

    /**
     * 获取Mine枚举
     *
     * @param value value
     * @return Mine枚举
     */
    public static MineEnum valueOfAuto(String value) {
        return Arrays.stream(MineEnum.values())
                .filter(mineEnum -> Stream.of(
                        mineEnum.name(),
                        mineEnum.getValue(),
                        mineEnum.getFileSuffix(),
                        mineEnum.getFileType().name(),
                        mineEnum.getFileType().getValue(),
                        mineEnum.getFileType().getLabel()
                ).anyMatch(v -> v.equalsIgnoreCase(value)))
                .findFirst()
                .orElse($OTHER);
    }

    @Override
    public String toString() {
        return "MineEnum{" +
                "value='" + value + '\'' +
                ", fileSuffix='" + fileSuffix + '\'' +
                ", fileType=" + fileType +
                '}';
    }
}
